import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.functions.Func0;

import java.io.IOException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by codeczx on 2017/2/15.
 */
public class ObservableSendEventTest {

    void testJust(){
        Observable<Integer> observable = Observable.just(1);
        Subscription subscription = observable.subscribe(v->System.out.println("Receive"+v));
    }

    void testInterval() throws IOException {
        Observable<Long> observable = Observable.interval(2,1, TimeUnit.SECONDS);
        Subscription subscription = observable.subscribe(v->System.out.println("Receive"+v));
        System.in.read();
    }

    void testDefer(){
        Observable<Long> now = Observable.defer(()->Observable.just(System.currentTimeMillis()));
        now.subscribe(System.out::println);
    }

    void testCreate(){
        Observable<String> values = Observable.create(f->{
            f.onNext("hello");
            f.onNext(" world!");
            f.onCompleted();
        });
        Subscription subscription = values.subscribe(System.out::print);
    }

    void testFrom(){
        FutureTask<Integer> task  = new FutureTask<Integer>(()->{
            Thread.sleep(2000);
            return 21;
        });
        new Thread(task).start();
        Observable<Integer> observable = Observable.from(task);
        Subscription subscription = observable.subscribe(
                v->System.out.println("Received:"+v),
                e->System.out.println("Error:"+e.getMessage()),
                ()->System.out.println("onCompleted")
        );
    }

    public static void main(String[] args) throws IOException {
        ObservableSendEventTest ost = new ObservableSendEventTest();
        ost.testJust();
//        ost.testInterval();
//        ost.testDefer();
//        ost.testCreate();
//        ost.testFrom();
    }
}
